<?php

/**
 * contact : 刘经理 19326416371
 * company : 版权所属 四川北星惯牌科技有限公司
 **/

namespace app\common\manager;

use app\admin\model\Order;
use app\common\model\User;
use think\Db;
use think\Exception;

class OrderManager extends BaseManager
{

    /**
     *
     * 发起订单支付
     * @param User $user
     * @param array $data
     * @param int $type 订单类型 1=购买体验卡,2=赛事,3=组队,4=充值
     * @param bool $trans
     * @return array
     */
    public static function Order(User $user,array $data,$type,bool $trans=true):array
    {
        if ($trans)
            Db::startTrans();
        try {
            $data['type'] = $type;
            $orderModel = new Order();
            $id = $orderModel->insertGetId($data);
            if (!$id){
                throw new Exception('订单创建失败');
            }
            if ($trans)
                Db::commit();
            self::setStatusCode(self::$successCode);
            self::setMessage(self::$successMessage);
            self::setData(['id'=>$id]);
        }catch (Exception $exception){
            if ($trans)
                Db::rollback();
            self::setStatusCode(self::$failCode);
            self::setMessage($exception->getMessage());
        }

        return ['code'=>self::getStatusCode(),'message'=>self::getMessage(),'data'=>self::getData()];
    }

    public static function modify($id,$data,$isTrans=true):array
    {
        if ($isTrans)
            Db::startTrans();
        try {
            \app\common\model\Order::update($data,['po_id'=>$id]);
            if ($isTrans)
                Db::commit();
            self::setStatusCode(self::$successCode);
            self::setMessage(self::$successMessage);
            self::setData(['id'=>$id]);
        }catch (Exception $e){
            if ($isTrans)
                Db::rollback();
            self::setStatusCode(self::$failCode);
            self::setMessage($e->getMessage());
        }

        return ['code'=>self::getStatusCode(),'message'=>self::getMessage(),'data'=>self::getData() ];
    }

    public static function UserOrder(User $user,$type,$status,$page=1,$step=10):array
    {
        $start = ($page-1)*$step;
        $return = ['total'=>0,'totalPage'=>1,'currentPage'=>$page,'list'=>[]];
        try {
            $where['user_id'] = $user->id;
            if ($type && in_array($type,\app\common\model\Order::TYPE_ARRAY)){
                $where['type'] = $type;
            }
            $orderModel = new Order();
            $count = $orderModel->where($where)->count();
            $info = $orderModel->where($where)->order('po_id desc')->field('po_id,order,pay_state,order_money,trilateral_pay_order,pay_date,expire_time,cancel_date,type,is_balance,ct_date')->limit($start,$step)->select();
            $return['list'] = $info;
            if ($info){
                foreach ($info as $k=>&$v){
                    if ($v->type){
                        $v->type_memo = \app\common\model\Order::TYPE_ARRAY[$v->type]['name'];
                    }
                    $v->status_memo = \app\common\model\Order::PAY_STATE_ARRAY[$v->pay_state]['name'];

                    $v->is_balance_memo = $v->is_balance ? '余额支付':'三方支付';
                }
            }
            $pageData = page($count,$step,$page);
            $return = array_merge($return,$pageData);
            self::setStatusCode(self::$successCode);
            self::setMessage(self::$successMessage);
            self::setData($return);
        }catch (Exception $exception){
            self::setStatusCode(self::$failCode);
            self::setMessage($exception->getMessage());
        }
        return ['code'=>self::getStatusCode(),'message'=>self::getMessage(),'data'=>self::getData()];
    }
}